// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
	//新增小组
	async addgroupAction(){
		let data = this.post();
		data.floor = 3;
		let orcode = await this.model('or_department')
			.where({id:this.shop_userinfo.departmentid})
			.getField('orcode','one'); 
		data.orcode = orcode[0];
		let res = await this.model('or_department').add(data);
		return res?this.success({info:"添加成功！"}):this.fail('添加失败！');
	}

	async grouplistAction(){
		let childlist = await this.getchilddepartment(this.shop_userinfo.departmentid);
		let alist = await this.model('or_department')
		.alias('d')
		.field('d.id,d.name,s.name as pname')
		.join('`think_or_department` s on d.pid = s.id')
		.where({'d.floor':3,'d.pid':['IN', childlist]})
		.select();
		let relist = {
			"draw": '',
			"data": alist,
			"breadcrumb":''
		}
		return this.json(relist);
	}

	async editgroupAction(){
		if(this.isPost){
			let data = this.post();
			let res = await this.model('or_department').where({id:data.id}).update(data);
			return res?this.success({info:"编辑成功！"}):this.fail('编辑失败！');
		}
		let id = this.get('id');
		//取出用户信息	
		let data = await this.model('or_department').where({id:id}).find();		
		return this.json(data);
	}

	async deletegroupAction(){
		let id = this.post('id');
		//判断有没有人员归属
		let haveuser = await this.model('or_app_user').where({departmentid:id}).find();
		if(!think.isEmpty(haveuser)){
			return this.fail('该分类有人员存在不能删除！');
		}
		let res = await this.model('or_department').where({id:id}).delete();
		return res?this.success({info:"删除成功！"}):this.fail('删除失败！');
	} 

	async groupnameAction(){
		let pid = this.shop_userinfo.departmentid;
		let alist = await this.model('or_department').where({floor:2,pid:pid}).select();
		return this.json(alist);
	}

}